#include "workerthread.h"
#include <QDebug>
#include <QElapsedTimer>
#include <QByteArray>

#include "mysingleton.h"
#include "cthreadsafebuffer.h"

WorkerThread::WorkerThread(quint64 len, bool enqueue, QObject *parent) :
    QThread(parent),
    m_len(len),
    m_enqueue(enqueue)
{

}

void WorkerThread::run()
{
    QElapsedTimer t;
    t.start();

    if (m_enqueue) {
        // 入队操作
        for (quint64 i = 0; i < m_len; ++i) {
            QByteArray data = QByteArray::number(i);
            mySingleton::instance().enqueue(data);
            emit dataProcessed("mySingleton enqueue:" + data);
        }
    } else {
        // 出队操作
        for (quint64 i = 0; i < m_len; ++i) {
            QByteArray data = mySingleton::instance().dequeue();
            emit dataProcessed("mySingleton dequeue:" + data);
        }
    }

    if (m_enqueue) {
        // 入队操作
        for (quint64 i = 0; i < m_len; ++i) {
            QByteArray data = QByteArray::number(i);
            CThreadSafeBuffer::getInstance()->enqueue(data);
            emit dataProcessed("enqueue:" + data);
        }
    } else {
        // 出队操作
        for (quint64 i = 0; i < m_len; ++i) {
            QByteArray data = CThreadSafeBuffer::getInstance()->dequeue();
            emit dataProcessed("dequeue:" + data);
        }
    }

    qDebug() << __TIME__ << __FUNCTION__ << __LINE__ << "currentThreadId:" << QThread::currentThreadId() << endl;
    qDebug() << __TIME__ << __FUNCTION__ << __LINE__ << "Elapsed time:" << t.elapsed() << endl;

    emit dataProcessed("操作完成");
}
